/**
 * <pre>
 * 紫光软件系统有限公司广电事业部
 * Copyright (C): 2011
 * 
 * 文件名称：
 * com.unis.iptv.client.RetryHandler.java
 * 
 * 文件描述: 
 * TODO 请求重试处理
 * 
 * Notes:
 * 修改历史(作者/日期/改动描述):
 * 杨飞宇/2011-8-8/初始化版本。
 * </pre>
 */
package com.unis.iptv.client;

import java.io.IOException;

import javax.net.ssl.SSLHandshakeException;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

/**
 * @date 2011-8-8
 * @author 杨飞宇
 */
public class RetryHandler implements HttpRequestRetryHandler {
	
	@Override
	public boolean retryRequest(IOException exception, int executionCount,
			HttpContext context) {
		if (executionCount >= 5) {
			// 如果大于等于最大重试次数返回false
			return false;
		}
		if (exception instanceof NoHttpResponseException) {
			return true;
		}
		if (exception instanceof SSLHandshakeException) {
			return false;
		}
		HttpRequest request = (HttpRequest) context
				.getAttribute(ExecutionContext.HTTP_REQUEST);
		boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
		if (idempotent) {
			return true;
		}
		return false;
	}
}
